package com.openingedu.mba.screen.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import com.actionbarsherlock.app.SherlockFragment;
import com.openingedu.mba.MainActivity;
import com.openingedu.mba.R;

public class LeftSlidingMenuFragment extends SherlockFragment implements
		OnClickListener {
	
	/** 消息 **/
	private View messageBtnLayout;
	/** 本地题库 **/
	private View storeLocalBtnLayout;
	/** 网络题库 **/
	private View storeServerBtnLayout;
	/** 练习及模拟 **/
	private View practiceBtnLayout;
//	/** 设置 **/
//	private View settingBtnLayout;
	
	private View orderBtnLayout;

	FragmentMessage messageContent;
	FragmentStoreLocal storeLocalContent;
	FragmentStoreServer storeServerContent;
	FragmentPractice practiceContent;
	FragmentSetting settingContent;
	FragmentOrder orderContent;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setRetainInstance(true);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.main_left_fragment, container,
				false);
		/** 消息 **/
		messageBtnLayout = view.findViewById(R.id.notificationBtnLayout);
		messageBtnLayout.setOnClickListener(this);
		/** 本地题库 **/
		storeLocalBtnLayout = view.findViewById(R.id.storeLocalBtnLayout);
		storeLocalBtnLayout.setOnClickListener(this);
		/** 网络题库 **/
		storeServerBtnLayout= view.findViewById(R.id.storeServerBtnLayout);
		storeServerBtnLayout.setOnClickListener(this);
		/** 练习及模拟 **/		
		practiceBtnLayout = view.findViewById(R.id.circleBtnLayout);
		practiceBtnLayout.setOnClickListener(this);
		/** 设置 **/
//		settingBtnLayout = view.findViewById(R.id.settingBtnLayout);
//		settingBtnLayout.setOnClickListener(this);
		
		orderBtnLayout = view.findViewById(R.id.orderBtnLayout);
		orderBtnLayout.setOnClickListener(this);
		
		// 设置默认选中Message
		messageBtnLayout.setSelected(true);
		storeLocalBtnLayout.setSelected(false);
		storeServerBtnLayout.setSelected(false);
		practiceBtnLayout.setSelected(false);
//		settingBtnLayout.setSelected(false);
		orderBtnLayout.setSelected(false);
		return view;
	}

	@Override
	public void onClick(View v) {
		SherlockFragment newContent = null;
		switch (v.getId()) {
		case R.id.notificationBtnLayout:
				messageContent = new FragmentMessage();
			newContent = messageContent;
			messageBtnLayout.setSelected(true);
			storeLocalBtnLayout.setSelected(false);
			storeServerBtnLayout.setSelected(false);
			practiceBtnLayout.setSelected(false);
//			settingBtnLayout.setSelected(false);
			orderBtnLayout.setSelected(false);
			break;
			
		case R.id.storeLocalBtnLayout:
				storeLocalContent = new FragmentStoreLocal();
			newContent = storeLocalContent;
			messageBtnLayout.setSelected(false);
			storeLocalBtnLayout.setSelected(true);
			storeServerBtnLayout.setSelected(false);
			practiceBtnLayout.setSelected(false);
//			settingBtnLayout.setSelected(false);
			orderBtnLayout.setSelected(false);
			break;
		case R.id.storeServerBtnLayout:
				storeServerContent = new FragmentStoreServer();
			newContent = storeServerContent;
			messageBtnLayout.setSelected(false);
			storeLocalBtnLayout.setSelected(false);
			storeServerBtnLayout.setSelected(true);
			practiceBtnLayout.setSelected(false);
//			settingBtnLayout.setSelected(false);
			orderBtnLayout.setSelected(false);
			break;
		case R.id.circleBtnLayout:
				practiceContent = new FragmentPractice();
			newContent = practiceContent;
			messageBtnLayout.setSelected(false);
			storeLocalBtnLayout.setSelected(false);
			storeServerBtnLayout.setSelected(false);
			practiceBtnLayout.setSelected(true);
//			settingBtnLayout.setSelected(false);
			orderBtnLayout.setSelected(false);
			break;
//		case R.id.settingBtnLayout:
//				settingContent = new FragmentSetting();
//			newContent = settingContent;
//			messageBtnLayout.setSelected(false);
//			storeLocalBtnLayout.setSelected(false);
//			storeServerBtnLayout.setSelected(false);
//			practiceBtnLayout.setSelected(false);
//			settingBtnLayout.setSelected(true);
//			break;
		case R.id.orderBtnLayout:
			switchOrderFg();
			return;
		default:
			break;
		}

		if (newContent != null) {
			switchFragment(newContent);
		}

	}
	
	public void switchOrderFg() {
		messageBtnLayout.setSelected(false);
		storeLocalBtnLayout.setSelected(false);
		storeServerBtnLayout.setSelected(false);
		practiceBtnLayout.setSelected(false);
		orderBtnLayout.setSelected(true);
		
		switchFragment(new FragmentOrder());
	}

	private void switchFragment(SherlockFragment fragment) {
		if (getActivity() == null) {
			return;
		}
		MainActivity ra = (MainActivity) getActivity();
		ra.switchContent(fragment);
	}
}
